﻿/*
	notes for next version:
		make usage same as trace() or alert(), no params needed
			1. new public method .setProperties(alert_obj)
			2. change defaults (isBlocker should be false not true as default, etc)
			3. change parameters of Alert from object to only single string of message
			
			so that usage can be PopUp.Alert("my string");
			   instead of PopUp.Alert({text:"<b>**CSS Error**</b>\nError opening URL \""+SCOPE_obj.shell.myXML.attributes.styleSheetURL+"\"",root:SCOPE_obj, textWidth:300, isBlocker:false});

			

// CHANGE LOG
mod 16 jun 07 - ddb
	ver 2.02a
	changed:
		
	added:
		toString() method
		className_str:String
		version_str:String

*/
// version 2.0
/*
	modified 11 Oct 2006 by ddb
		changed:
			private static var Text;
		to:
			private static var Text:Textfield;
		changed:
			Text = Clip.createEmptyTextField(...);
		to:
			Clip.createEmptyTextField(...);
			Text = Clip["alert"+alertID+"_txt"];

	modified 21 Aug 2006 by ddb
		added isSelectable property
		
*/
/*
// METHOD TABLE:
//  methods
//
private function PopUp() {
public static function Alert(alertObj:Object) {
private static function drawRectangle(theClip:MovieClip, cornerRadius:Array):Void {
private static function setDefaultTF():TextFormat {
private static function makeCloseable():Void {
private static function makeDraggable():Void {
private function toString():Void{
*/
/*
		var alertObj:Object = new Object();
		alertObj.root = this;
		alertObj.text = "testing PopUp.Alert()";
		alertObj.textWidth = 200;
		alertObj.cornerRadius = [8,8,8,8];
		alertObj.whitespace = 4;
		alertObj.fillColor = 0xffffff;
		alertObj.strokeColor = 0x000000;
		alertObj.strokeWidth = 2;
		alertObj.txtFormat;
			// TextFormat(font:String, size:Number, color:Number, bold:Boolean, italic:Boolean, underline:Boolean, url:String, target:String, align:String, leftMargin:Number, rightMargin:Number, indent:Number, leading:Number)
		alertObj.coords;
		alertObj.isAlignedCenter = true;
		alertObj.closeCallBack;
		alertObj.isDraggable = true;
		alertObj.isBlocker = true;
		alertObj.symbolLinkage;
		PopUp.Alert(alertObj);

*/

class com.darrendb.as2.utils.PopUp {
	private var className_str:String;
	private var version_str:String;
	//
	// properties
	private static var root:MovieClip;
	private static var alertID:Number;
	private static var alertText:String;
	private static var textFieldWidth:Number;
	private static var isSelectable:Boolean;
	private static var cornerRadius:Array;
	private static var whitespace:Number;
	private static var fillColor:Number;
	private static var strokeColor:Number;
	private static var strokeWidth:Number;
	private static var Clip:MovieClip;
	private static var Blocker:MovieClip;
	private static var Text:TextField;
	private static var txtFormat:TextFormat;
	private static var isAlignedCenter:Boolean;
	private static var coords:Array;
	private static var closeCallback:Function;
	private static var isDraggable:Boolean;
	private static var isBlocker:Boolean;
	private static var symbolLinkage:String;
	// constructor
	private function PopUp() {
		className_str = "  PopUp";
		version_str = "v2.02";
		trace(this);
	}
	public static function Alert(alertObj:Object) {
		trace("PopUp.Alert()");
		// validate paramaters
		if (alertObj.root == null || alertObj.root == undefined) {
			alertObj.root = _root;
		}
		if (alertObj.text == null || alertObj.text == undefined) {
			alertObj.text = "alertObj.text== null || == undefined";
		}
		if (alertObj.textWidth == null || alertObj.textWidth == undefined) {
			alertObj.textWidth = 200;
		}
		if (alertObj.isSelectable == null || alertObj.isSelectable == undefined) {
			alertObj.isSelectable = false;
		}
		if (alertObj.cornerRadius == null || alertObj.cornerRadius == undefined) {
			alertObj.cornerRadius = new Array(0,0,0,0);
		}
		if (alertObj.whitespace == null || alertObj.whitespace == undefined) {
			alertObj.whitespace = 4;
		}
		if (alertObj.fillColor == null || alertObj.fillColor == undefined) {
			alertObj.fillColor = 0xffffcc;
		}
		if (alertObj.strokeColor == null || alertObj.strokeColor == undefined) {
			alertObj.strokeColor = 0x000000;
		}
		if (alertObj.strokeWidth == null || alertObj.strokeWidth == undefined) {
			alertObj.strokeWidth = 1;
		}
		alertObj.txtFormat = (alertObj.txtFormat == true) ? alertObj.txtFormat : setDefaultTF();
		alertObj.txtFormat = alertObj.txtFormat ? alertObj.txtFormat : setDefaultTF();
		if (alertObj.txtFormat == null || alertObj.txtFormat == undefined) {
			alertObj.txtFormat = setDefaultTF();
		}
		if (alertObj.isBlocker == null || alertObj.isBlocker == undefined) {
			alertObj.isBlocker = false;
		}
		if (alertObj.isAlignedCenter == null || alertObj.isAlignedCenter == undefined) {
			alertObj.isAlignedCenter = true;
		}
		if (alertObj.isDraggable == null || alertObj.isDraggable == undefined) {
			alertObj.isDraggable = true;
		}
		
		// assign properties from paramaters 
		root = alertObj.root;
		alertText = alertObj.text;
		textFieldWidth = alertObj.textWidth;
		isSelectable = alertObj.isSelectable;
		cornerRadius = alertObj.cornerRadius;
		whitespace = alertObj.whitespace;
		fillColor = alertObj.fillColor;
		strokeColor = alertObj.strokeColor;
		strokeWidth = alertObj.strokeWidth;
		txtFormat = alertObj.txtFormat;
		isAlignedCenter = alertObj.isAlignedCenter;
		coords = alertObj.coords;
		closeCallback = alertObj.closeCallBack;
		isDraggable = alertObj.isDraggable;
		isBlocker = alertObj.isBlocker;
		symbolLinkage = alertObj.symbolLinkage;
		// alertID used in instance name
		if (alertID == null || alertID == undefined){
			alertID = 0;
		} else {
			alertID++;
		}
		
		// create blocker_mc
		if (isBlocker) {
			Blocker = root.createEmptyMovieClip("blocker"+alertID+"_mc", alertID);
			alertID++
			drawRectangle(Blocker,[0,0,0,0]);
			Blocker.onRollOver = function(){this.useHandCursor = false;};
			Blocker._alpha = 0;
		}
		// create mc
		Clip = root.createEmptyMovieClip("alert"+alertID+"_mc", alertID);
		Clip.suicide = function(){
			removeMovieClip(this);
			removeMovieClip(Blocker);
			closeCallback.apply();
		};
		Clip.Blocker = Blocker;
		
		// create textfield
		Clip.createTextField("alert"+alertID+"_txt", 1, whitespace, whitespace, textFieldWidth, 200);
		Text = Clip["alert"+alertID+"_txt"];
		// set textField properties
		Text.type = "dynamic";
		Text.border = false;
		Text.background = false;
		Text.password = false;
		Text.multiline = true;
		Text.html = true;
		Text.embedFonts = false;
		//Text.selectable = false;
		Text.selectable = isSelectable;
		Text.wordWrap = true;
		Text.mouseWheelEnabled = true;
		Text.condenseWhite = false;
		Text.restrict = null;
		Text.variable = null;
		Text.maxChars = null;
		Text.styleSheet = undefined;
		Text.tabEnabled = undefined;
		Text.autoSize = true;
		// set text
		Text.htmlText = alertText;
		// apply textFormat to textField
		Text.setTextFormat(txtFormat);
		// adjust textField height
		Clip._height = Text.textHeight+4;
		// had to add 4 to fit last line (??)
		// adjust textField pos
		Clip._x = Math.round(Clip._x);
		// ensure textField is on whole pixel 
		Clip._y = Math.round(Clip._y);
		// (rumor: textField displays fuzzy when not on whole pixel)
		// set mc dims
		Clip.innerWidth = textFieldWidth+(whitespace*2);
		Clip.innerHeight = Clip._height+(whitespace*2);
		// Alert background
		if (symbolLinkage == null || symbolLinkage == undefined) {
			// draw background
			drawRectangle(Clip, cornerRadius);
		} else {
			// attach background
			Clip.attachMovie(symbolLinkage,"alertLinkage"+alertID+"_mc",-1);
		}
		// position
		if (coords.length == 2) {
			Clip._x = coords[0];
			Clip._y = coords[1];
		} else if (isAlignedCenter) {
			//			Clip._x = Align.CenterX(Clip, _level0, true);
			//			Clip._y = Align.CenterY(Clip, _level0, true);
			var clipX:Number = com.darrendb.as2.utils.Align.CenterX(Clip, _level0, true);
			var clipY:Number = com.darrendb.as2.utils.Align.CenterY(Clip, _level0, true);
			//trace("clipX:"+clipX);
			//trace("clipY:"+clipY);
			Clip._x = clipX;
			Clip._y = clipY;
		}
		// Mouse event handlers 
		if (isDraggable) {
			makeDraggable();
		} else {
			makeCloseable();
		}
			
	}
	private static function drawRectangle(theClip:MovieClip, cornerRadius:Array):Void {
		if (strokeWidth == undefined) strokeWidth = 1;
		if (strokeColor == undefined) strokeColor = 0xFFFFFF;
		if (fillColor == undefined) fillColor = 0xFFFFFF;
		if (theClip.innerWidth == undefined) theClip.innerWidth = Stage.width;
		if (theClip.innerHeight == undefined) theClip.innerHeight = Stage.height;
		
		// set stroke
		theClip.lineStyle(strokeWidth, strokeColor, 100);
		// set fill
		theClip.beginFill(fillColor);
		// upper left corner
		theClip.moveTo(0, cornerRadius[0]);
		theClip.curveTo(0, 0, cornerRadius[0], 0);
		// top side
		theClip.lineTo((theClip.innerWidth-cornerRadius[1]), 0);
		// upper right corner
		theClip.curveTo(theClip.innerWidth, 0, theClip.innerWidth, cornerRadius[1]);
		// right side
		theClip.lineTo(theClip.innerWidth, (theClip.innerHeight-cornerRadius[2]));
		// bottom right corner
		theClip.curveTo(theClip.innerWidth, theClip.innerHeight, (theClip.innerWidth-cornerRadius[2]), theClip.innerHeight);
		// bottom side
		theClip.lineTo(cornerRadius[3], theClip.innerHeight);
		// bottom left corner
		theClip.curveTo(0, theClip.innerHeight, 0, (theClip.innerHeight-cornerRadius[3]));
		// left side
		theClip.lineTo(0, cornerRadius[0]);
		theClip.endFill();
	}
	private static function setDefaultTF():TextFormat {
		// create textFormat
		var txtFormat:TextFormat = new TextFormat();
		txtFormat.color = 0x000000;
		txtFormat.font = "_serif";
		txtFormat.size = 12;
		txtFormat.bold = null;
		// if set to null, no change is made to that property
		txtFormat.italic = null;
		// if set to true/false, overrides any html formatting
		txtFormat.underline = null;
		// ( i.e. <b>Hello</b> )
		txtFormat.url = null;
		txtFormat.target = null;
		txtFormat.align = "left";
		txtFormat.leftMargin = 2;
		txtFormat.rightMargin = 2;
		txtFormat.indent = 0;
		txtFormat.leading = 0;
		txtFormat.blockIndent = 0;
		txtFormat.bullet = null;
		txtFormat.tabStops = [];
		// (empty array)
		return txtFormat;
	}
	private static function makeCloseable():Void {
		Clip.onRelease = function() {
			// close
			this.suicide();
		};
	}
	private static function makeDraggable():Void {
		Clip.onPress = function() {
			if (this.lastClick - (this.lastClick = getTimer()) + 400 > 0){
				// doubleClick
				// close
				this.suicide();
			} else {
				// singleClick
				// drag
				startDrag(this);
			}
		};
		Clip.onRelease = Clip.onReleaseOutside = function() {
			stopDrag();
		};
	}
	private function toString():String {
		return className_str + " ("+version_str+")";
	}
}
